/*
 * DiagramElementMoveCommand.java
 *
 * Created on 12 czerwiec 2007, 20:25
 */

package edu.uj.petri.commands;

import edu.uj.petri.document.Document;
import edu.uj.petri.view.ArcView;
import edu.uj.petri.view.DiagramElement;
import edu.uj.petri.view.NodeView;
import java.util.ResourceBundle;

/**
 *
 * @author Piotrek
 */
public class DiagramElementMoveCommand extends Command {
    
    private static final long serialVersionUID = 2841748299713507670L;
    
    private DiagramElement element;
    private Memento initialState, resultState;
    private boolean firstTime=true;
    
    /**
     * Creates a new instance of DiagramElementMoveCommand
     */
    public DiagramElementMoveCommand(DiagramElement element, Memento initialState) {
        this.element = element;
        this.initialState = initialState;
        this.resultState = element.getMemento();
    }

    public String toString() {
        String what=null;
        if (element instanceof NodeView) {
            what = ((NodeView)element).getNode().getName();
        } else {
            what = ResourceBundle.getBundle("edu/uj/petri/messages").getString("(of)diagram_element");
        }
        return String.format(ResourceBundle.getBundle("edu/uj/petri/messages").getString("moving") + " " + what);
    }

    void execute() {
        if (firstTime) {
            // do nothing, already moved
            firstTime = false;
        } else {
            element.applyMemento(resultState);
        }
        Document.getActiveDocument().getView().draw();
    }

    void undo() {
        element.applyMemento(initialState);
        Document.getActiveDocument().getView().draw();
    }

    boolean canBeCombined(Command c) {
        if (! (c instanceof DiagramElementMoveCommand) )
            return false;
        DiagramElementMoveCommand another = (DiagramElementMoveCommand)c;
        return (!(element instanceof ArcView) && another.element == this.element);
    }

    void combine(Command c) {
        DiagramElementMoveCommand another = (DiagramElementMoveCommand)c;
        this.firstTime = true;
        resultState = another.resultState;
    }

    
}
